﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SkeletonNET.Base.Crud.UnitOfWork;
using System.Data.Entity;
using System.Reflection;
using Skeleton.Data.Tests;

namespace SkeletonNET.Data.Tests
{
    public class FakeDbContext : EfContextBase
    {
        public IDbSet<TestEntity> TestEntity { get; set; }
        public IDbSet<TestEntity2> TestEntity2 { get; set; }
        public new IDbSet<T> Set<T>() where T : class
        {
            foreach (PropertyInfo property in typeof(FakeDbContext).GetProperties())
            {
                if (property.PropertyType == typeof(IDbSet<T>))
                    return property.GetValue(this, null) as IDbSet<T>;
            }
            throw new Exception("Type collection not found");
        }

        public new void SaveChanges()
        {
            // do nothing (probably set a variable as saved for testing)
        }

        public FakeDbContext()
        {
            TestEntity = new FakeDbSet<TestEntity>() { new TestEntity() };
            TestEntity2 = new FakeDbSet<TestEntity2>() { new TestEntity2() };
        }

        public override IQueryable<TEntity> GetQuery<TEntity>()
        {
            return (IQueryable<TEntity>)Set<TEntity>();
        }

        public override TEntity Add<TEntity>(TEntity entity)
        {
            return Set<TEntity>().Add(entity);
        }

        public override void Remove<TEntity>(TEntity entity)
        {
            Set<TEntity>().Remove(entity);
        }
    }
}